Skip to main content

Exhaustive dependencies for query keys


该规则要求将 query key 视为查询函数的依赖项数组:在 queryFn 中使用的每个变量都应该添加到 query key 中。这样可以确保查询独立缓存,并在变量发生更改时自动重新获取查询。

规则详情

以下是此规则不正确的代码示例:

/* eslint "@tanstack/query/exhaustive-deps": "error" */

useQuery({
queryKey: ["todo"],
queryFn: () => api.getTodo(todoId)
})

const todoQueries = {
detail: (id) => ({ queryKey: ["todo"], queryFn: () => api.getTodo(id) })
}

以下是此规则正确的代码示例:

useQuery({
queryKey: ["todo", todoId],
queryFn: () => api.getTodo(todoId)
})

const todoQueries = {
detail: (id) => ({ queryKey: ["todo", id], queryFn: () => api.getTodo(id) })
}

什么时候不使用该规则

如果您不关心查询键的规则,则不需要使用此规则。

属性

  • ✅ 推荐
  • 🔧 可修复